;
var helper = require("../../javascript/helper");
var request = require("request");
var format = require("string-format");
var db = require("../../javascript/mysql");
var mysql = new db.mysqlClient("accident");
var spider = function () {
    var that = this;
    this.name = "公共交通-史带综合意外保险"
    //生成产品数据
    this.genProductInfo = function () {
        let projects = [];
        plan = {
            planA: {
                "year": ["1-80"],
                "days": ["3d", "1y"],
                "people_count": ["1"],
                "airplane_death": ["500000"],
                "train_death": ["200000"],
                "ship_death": ["200000"],
                "passenger_death": ["100000"],
                "common_traffic": ["10000"]
            },
            planB: {
                "year": ["1-80"],
                "days": ["3d", "1y"],
                "people_count": ["1"],
                "airplane_death": ["1000000"],
                "train_death": ["500000"],
                "ship_death": ["500000"],
                "passenger_death": ["200000"],
                "common_traffic": ["20000"]
            },
            planC: {
                "year": ["1-80"],
                "days": ["3d", "1y"],
                "people_count": ["1"],
                "airplane_death": ["2000000"],
                "train_death": ["800000"],
                "ship_death": ["800000"],
                "passenger_death": ["300000"],
                "common_traffic": ["50000"]
            },
            planD: {
                "year": ["1-80"],
                "days": ["3d", "1y"],
                "people_count": ["1"],
                "airplane_death": ["3000000"],
                "train_death": ["1000000"],
                "ship_death": ["1000000"],
                "passenger_death": ["300000"],
                "common_traffic": ["100000"]

            }
        }
        for (k in plan) {
            data = helper.product([plan[k]["year"], plan[k]["days"], plan[k]["people_count"], plan[k]["airplane_death"], plan[k]["train_death"], plan[k]["ship_death"], plan[k]["passenger_death"], plan[k]["common_traffic"]])
            for (i = 0; i < data.length; i++) {
                dt = helper.formatProductInfo(data[i], plan[k]);
                projects.push(dt);
            }
        }
        return projects;
    }
    // 执行请求
    this.httpRequest = function ( dt ) {
        if(dt.airplane_death == "500000"){
            planCd = "2128"
        }else if (dt.airplane_death=="1000000"){
            planCd = "2129"
        }else if (dt.airplane_death == "2000000"){
            planCd = "2130"
        }else {
            planCd = "2131"
        }
        if ( dt.days.indexOf("y") != -1){
            params = {
                "DateStart": "2020-05-30",
                "DateEnd": "2021-05-29",
                "unit": "year",
                "_curcid": "16008"
            }
        }else{
            params = {
                "DateStart": "2020-05-30",
                "DateEnd": "2020-06-01",
                "unit": "day",
                "_curcid": "16008"
            }

        }
        return new Promise(function (resolve, reject) {
            request({
                url: format("http://mall.starrchina.cn/ah/plans/{0}/fee?{1}",planCd,helper.urlencode( params )),
                method: "GET",
                json: false,
                rejectUnauthorized: false,
                headers: {
                    "Accept": "application/json, text/javascript, */*; q=0.01",
                    "Accept-Language": "zh-CN,zh;q=0.9",
                    "Connection": "keep-alive",
                    "Content-Type": "application/json",
                    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.113 Safari/537.36",
                    "X-Requested-With": "XMLHttpRequest"

                }
            }, function (err, res, body) {
                console.log( format("http://mall.starrchina.cn/ah/plans/{0}/fee?{1}",planCd,helper.urlencode( params )) )
                console.log( params );
               
                if (!err && res.statusCode == 200) {
                    if(body.indexOf("Fee")!=-1){
                        price = JSON.parse( body ).data[0].Fee.toFixed(2).toString();
                        rs = {
                            name:that.name,
                            company:"史带财险",
                            price:price
                        }
                       
                        resolve( rs );
                    }

                }
            })

        })
    }
    //执行主函数
    this.main = async function( ){
        data = that.genProductInfo();
        for(i=0;i<data.length;i++){
            dt = data[i]
            let rs = await that.httpRequest( dt )
            result = Object.assign( dt,rs )
            mysql.insertData( result );
            
        }
        mysql.close();

    }
}
